﻿using UnityEngine;
using UnityEngine.Profiling;

namespace IslandMatch
{
    /// <summary>
    /// 显示fps组件
    /// </summary>
    public class FpsComponent : MonoBehaviour
    {
        //fps
        private static int m_fps;
        private float m_elapseTime;
        private int m_frame;
        private long memory = 0;

        private GUIStyle style;

        public static bool Enable = false;

        public static int GetFps()
        {
            return m_fps;
        }

        void Update()
        {
            UpdateFps();
            UpdateMemory();
        }

        void UpdateMemory()
        {
            memory = Profiler.GetTotalAllocatedMemoryLong();

            // long _memory = Profiler.GetTotalAllocatedMemoryLong();
            // float addMemory = (_memory - memory) / 1024f / 1024f;
            //
            // if (Math.Abs(addMemory) > 0.01f)
            //     memory = _memory;
        }

        void UpdateFps()
        {
            m_elapseTime += Time.deltaTime / Time.timeScale;
            ++m_frame;
            if (m_elapseTime >= 1.0f)
            {
                m_fps = (int)(m_frame / m_elapseTime);
                m_frame = 0;
                m_elapseTime = 0;
            }
        }

        double mFrameCount = 0;
        const uint kNumFrameTimings = 2;
        FrameTiming[] frameTimings = new FrameTiming[3];
        double m_gpuFrameTime;
        double m_cpuFrameTime;

        void DetermineResolution()
        {
            ++mFrameCount;
            if (mFrameCount <= kNumFrameTimings)
            {
                return;
            }

            FrameTimingManager.CaptureFrameTimings();
            FrameTimingManager.GetLatestTimings(kNumFrameTimings, frameTimings);
            if (frameTimings.Length < kNumFrameTimings)
            {
                Debug.Log($"Skipping frame {mFrameCount}, didn't get enough frame timings.");
                return;
            }

            m_gpuFrameTime = (double)frameTimings[0].gpuFrameTime;
            m_cpuFrameTime = (double)frameTimings[0].cpuFrameTime;
        }

        private void OnGUI()
        {
            if (style == null)
            {
                style = new GUIStyle();
                style.fontSize = 32;
            }

            if (m_fps < 30)
            {
                style.normal.textColor = Color.red;
                GUI.Label(new Rect(0, 0, 400f, 400f), $"FPS: {m_fps.ToString()}", style);
            }
            else
            {
                style.normal.textColor = Color.green;
                GUI.Label(new Rect(0, 0, 400f, 400f), $"FPS: {m_fps.ToString()}", style);
            }

            string memoryStr =
                $"已占用内存: {memory / 1024f / 1024f}MB\r\n总内存: {Profiler.GetTotalReservedMemoryLong() / 1024f / 1024f}MB\r\n空闲内存{Profiler.GetTotalUnusedReservedMemoryLong() / 1024f / 1024f}MB";


            //memoryStr = $"{memoryStr}\nGPU: {m_gpuFrameTime}\nCPU: {m_cpuFrameTime}";

            GUI.Label(new Rect(0, 50f, 1000f, 400f), memoryStr, style);
        }
    }
}